$(() => {
    const token = GetQueryString("token");
    showUserMsg();

    // 地址三级联动
    $('.cityList').on('click', function() {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Api|get_area_list', 'pcode': 1 },
            dataType: 'json',
            success: function(res) {
                // // console.log(res);
                let data = res.data;
                let province = '';
                if (res.status == 1) {
                    data.forEach(item => {
                        province += `
                            <a href="javascript:;" data-code="${item.area_code}" class="h88 cityItem">${item.area_name}</a>
                         `;
                    })
                    $('.cityLists').html(province);
                    $(".cityItem").on('click', function() {
                        // // console.log($(this).attr('data-code'));
                        $('.province').val($(this).html());
                        let area_code = $(this).attr('data-code');
                        $('.province').attr('data-code', area_code);
                        $.ajax({
                            type: "post",
                            url: url,
                            data: { 'method': 'Api|get_area_list', 'pcode': area_code },
                            dataType: 'json',
                            success: function(res) {
                                // // console.log(res);
                                let data = res.data;
                                let city = '';
                                if (res.status == 1) {
                                    data.forEach(item => {
                                        city += `
                                        <a href="javascript:;" data-code="${item.area_code}" class="h88 cityItem">${item.area_name}</a>
                                         `;
                                    })
                                    $('.cityLists').html(city);
                                    $(".cityItem").on('click', function() {
                                        $('.city').val($(this).html());
                                        let area_code = $(this).attr('data-code');
                                        $('.city').attr('data-code', area_code);
                                        $.ajax({
                                            type: "post",
                                            url: url,
                                            data: { 'method': 'Api|get_area_list', 'pcode': area_code },
                                            dataType: 'json',
                                            success: function(res) {
                                                // // console.log(res);
                                                let data = res.data;
                                                let county = '';
                                                if (res.status == 1) {
                                                    data.forEach(item => {
                                                        county += `
                                                        <a href="javascript:;" data-code="${item.area_code}" class="h88 cityItem">${item.area_name}</a>
                                                         `;
                                                    })
                                                    $('.cityLists').html(county);
                                                    $('.cityItem').on('click', function() {
                                                        let area_code = $(this).attr('data-code');
                                                        $('.county').attr('data-code', area_code);
                                                        let province = $('.province').val();
                                                        let city = $('.city').val();
                                                        let conuty = $(this).html();
                                                        $('#ringadtext2').val(province + '-' + city + '-' + conuty);
                                                        dialogClose();
                                                    })
                                                }
                                            }
                                        })
                                    })
                                }
                            }
                        })
                    })
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    })

    // 个人信息显示
    function showUserMsg() {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'User|get_user_private', 'token': token },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    // 认证不能编辑信息
                    if (data.company_confirm == 1) {
                        $('.saveBtn').hide();
                        $('#fileInput').attr('type', 'text');
                        $('.perLists input').attr('readonly', '');
                        $('.perLists textarea').attr('readonly', '');
                        $('.cityList').attr('href', 'javascript:;');
                    }
                    $('#headImg').attr('src', data.img_head);
                    $('.nick_name').val(data.nick_name);

                    if (data.phone != '') {
                        $('.phone').hide();
                        $('.phone1').css('display', 'flex');
                        $('.phone1').html(data.phone);
                    }
                    // $('.address').val(data.address);
                    if (data.main_business != '') {
                        $('.main_business').hide();
                        $('.main_business1').css('display', 'flex');
                        $('.main_business1').html(data.main_business);
                    }
                    $('.description').val(data.description);
                    if (data.address != '') {
                        $('.address').hide();
                        $('.arealist').css('display', 'flex');
                        $('.arealist').html(data.address);
                    }
                    $('#ringadtext2').val(data.area_address);
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }

            }
        })
    }

    // 个人信息保存
    $('.saveBtn').on('click', function() {
        let img_head = $('#headImg').attr('src');
        let nick_name = $('.nick_name').val();
        let address = $('.address').val();
        let phone = $('.phone').val();
        let main_business = $('.main_business').val();
        let description = $('.description').val();
        let province_code = $('.province').attr('data-code');
        let city_code = $('.city').attr('data-code');
        let country_code = $('.county').attr('data-code');
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'User|user_private_save', 'token': token, 'img_head': img_head, 'nick_name': nick_name, 'address': address, 'phone': phone, 'main_business': main_business, 'description': description, 'province_code': province_code, 'city_code': city_code, 'country_code': country_code },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    alert(res.message);
                    showUserMsg();
                }
            }
        })
    })

})